package com.supermap.test;


import com.supermap.data.*;
import com.supermap.data.conversion.*;


import java.io.File;
import java.util.ArrayList;
import java.util.List;

public class VCTExporter {
    private static Datasource datasource = null;

    private Datasource tempDatasource;

    public VCTExporter(Datasource datasource) {
        this.datasource = datasource;
    }

    public static void main(String[] args) throws Exception {

        Workspace workspace = new Workspace();
        WorkspaceConnectionInfo conInfo = new WorkspaceConnectionInfo(
                "E:\\MapData\\202411\\陕西省(61)DGAF工程文件(20241030174750)\\陕西省(61)DGAF工程文件(20241030174750).smwu");
        conInfo.setType(WorkspaceType.SMWU);
        workspace.open(conInfo);
        datasource= workspace.getDatasources().get(0);
        Dataset dataset = datasource.getDatasets().get("dltb_j_61");
        exportVCT(dataset, "E:\\MapData\\202411\\陕西省(61)DGAF工程文件(20241030174750)\\陕西省(61)DGAF工程文件(20241030174750).vct");

    }

    private static List<DatasetVector> getExportDatasetVectors() throws Exception {
        List<DatasetVector> sourceVectorList = new ArrayList<>();
        for (int i = 0; i < datasource.getDatasets().getCount(); i++) {
            Dataset dataset = datasource.getDatasets().get(i);
            if (dataset instanceof DatasetVector) {
                sourceVectorList.add((DatasetVector) dataset);
            }
        }

        return sourceVectorList;
    }

    private static void exportVCT(Dataset dataset, String vctPath) throws Exception {
        if (!new File(vctPath).exists()) {
            new File(vctPath).createNewFile();
        }

        ExportSettingVCT exportSettingVCT = new ExportSettingVCT();
        exportSettingVCT.setVersion(VCTVersion.LANDUSE_VCT30);
        exportSettingVCT.setOverwrite(true);
        exportSettingVCT.setTargetFilePath(vctPath);
        exportSettingVCT.setTargetFileCharset(Charset.UTF8);
        exportSettingVCT.setTargetFileType(FileType.VCT);
        exportSettingVCT.setOverwrite(true);
        exportSettingVCT.setConfigFilePath("E:\\MapData\\202411\\陕西省(61)DGAF工程文件(20241030174750)\\dltb_j_61.config");
        exportSettingVCT.setSourceData(dataset);
        //exportSettingVCT.setSourceDatas(datasetVectorList.toArray());
        DataExport dataExport = new DataExport();
        dataExport.getExportSettings().add(exportSettingVCT);
        dataExport.addExportSteppedListener(exportSteppedEvent ->
        {
            System.out.println(exportSteppedEvent.getTotalPercent());
            System.out.println(exportSteppedEvent.getCount());
        });
        ExportResult exportResult = dataExport.run();

        if (exportResult.getFailedSettings() != null && exportResult.getFailedSettings().length > 0) {
            System.err.println("exportResult.getFailedSettings()[0]");
            System.out.println(exportResult.getFailedSettings()[0]);
        }
        dataExport.dispose();
    }
}
